<?php

class Application_Model_lailaiACS4MD
{
	private $DBTable_lai;
	private $bookListFName;	//LaiLai書籍清單的檔名。
	private $pathToSave;	//儲存和LaiLai相關資料的資料夾名稱。
	private $serverUrl;		//LaiLai伺服器位置。
	private $bookListUrl;	//TKQW書籍清單位置。
	
	public function __construct()
	{
		$this->DBTable_lai = new Zend_Db_Table('lailaiACS4');
		$this->bookListFName = 'books.xml';
		$this->serverUrl = 'http://211.21.48.204/ACS4_Share/';
		$this->bookListUrl = $this->serverUrl . 'BOOKS/books.xml';
		
		//如果還未建立資料夾"lailaiACS4"，就建立它吧！
		$this->pathToSave = 'lailaiACS4';
		if(FALSE === is_dir($this->pathToSave))
		{
			mkdir($this->pathToSave);
		}
	}
	
	/**
	 * 到LaiLai ACS4伺服器取得書籍清單，並儲存到資料夾"lailai"底下。
	 * @param none
	 * 
	 * @return $ary_rsp['isSuccess'] 是否成功取得書籍清單？
	 * @return $ary_rsp['errMsg'] 錯誤訊息，如果無錯誤，預設為空字串。
	 */
	public function curlBookList()
	{
		$ch = curl_init($this->bookListUrl);
		$fp = fopen($this->pathToSave . '/' . $this->$bookListFName, 'w');
		
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_FILE, $fp);
		
		$isSuccess = curl_exec($ch);
		if(FALSE === $isSuccess)
		{
			$ary_rsp['isSuccess'] = FALSE;
			$ary_rsp['errMsg'] = curl_error($ch);
		}
		elseif(TRUE === $isSuccess)
		{
			$ary_rsp['isSuccess'] = TRUE;
			$ary_rsp['errMsg'] = '';
		}
		
		curl_close($ch);
		fclose($fp);
		
		return $ary_rsp;
	}
	
	/**
	 * 解析書籍清單內的xml內容，並且將資料寫入DB。
	 * @param none
	 * 
	 * @return none
	 */
	public function add($pathToBookList='lailaiACS4/books.xml')
	{
		if(file_exists($pathToBookList))
		{
		    $xml = @simplexml_load_file($pathToBookList);
		    if(FALSE !== $xml)
		    {
		    	$ary_addData = array();
			    foreach($xml as $eachBook)
			    {
		    		$ary_addData['lai_resource'] = isset($eachBook->resource)?
		    		(string)$eachBook->resource : '';
		    		$ary_addData['lai_resourceItem'] = isset($eachBook->resourceItem)?
		    		(string)$eachBook->resourceItem : '';
		    		$ary_addData['lai_src'] = isset($eachBook->src)?
		    		(string)$eachBook->src : '';
		    		$ary_addData['lai_downloadType'] = isset($eachBook->downloadType)?
		    		(string)$eachBook->downloadType : '';
		    		
		    		$xml_metadata = isset($eachBook->metadata)?
		    		$eachBook->metadata : NULL;
		    		if(FALSE === is_null($xml_metadata))
		    		{
		    			$ary_addData['lai_thumbnailURL'] = isset($xml_metadata->thumbnailURL)?
		    			(string)$xml_metadata->thumbnailURL : '';
		    			
		    			$xml_ns = $xml_metadata->children('dc', TRUE);
		    			if(! empty($xml_ns))
		    			{
		    				$ary_addData['lai_title'] = isset($xml_ns->title)?
		    				(string)$xml_ns->title : '';
		    				$ary_addData['lai_creator'] = isset($xml_ns->creator)?
		    				(string)$xml_ns->creator : '';
		    				$ary_addData['lai_format'] = isset($xml_ns->format)?
		    				(string)$xml_ns->format : '';
		    				$ary_addData['lai_publisher'] = isset($xml_ns->publisher)?
		    				(string)$xml_ns->publisher : '';
		    				$ary_addData['lai_language'] = isset($xml_ns->language)?
		    				(string)$xml_ns->language : '';
		    				$ary_addData['lai_description'] = isset($xml_ns->description)?
		    				(string)$xml_ns->description : '';
		    				$ary_addData['lai_identifier'] = isset($xml_ns->identifier)?
		    				(string)$xml_ns->identifier : '';
		    			}
		    		}
		    		
		    		$xml_licenseToken = isset($eachBook->licenseToken)?
		    		$eachBook->licenseToken : NULL;
		    		if(FALSE === is_null($xml_licenseToken))
		    		{
		    			$ary_addData['lai_displayDevice'] = isset($xml_licenseToken->permissions->display->device)?
		    			(string)$xml_licenseToken->permissions->display->device : '';
		    			$ary_addData['lai_displayUntil'] = isset($xml_licenseToken->permissions->display->until)?
		    			(string)$xml_licenseToken->permissions->display->until : '';
		    			$ary_addData['lai_excerptUntil'] = isset($xml_licenseToken->permissions->excerpt->until)?
		    			(string)$xml_licenseToken->permissions->excerpt->until : '';
		    			$ary_addData['lai_printMaxResolution'] = isset($xml_licenseToken->permissions->print->maxResolution)?
		    			(string)$xml_licenseToken->permissions->print->maxResolution : '';
		    			
		    			$xml_printCount = isset($xml_licenseToken->permissions->print->count)?
		    			$xml_licenseToken->permissions->print->count : NULL;
		    			if(FALSE === is_null($xml_printCount))
			    		{
			    			$ary_addData['lai_printCountInitial'] = isset($xml_printCount->attributes()->initial)?
			    			(string)$xml_printCount->attributes()->initial : 0;
			    			$ary_addData['lai_printCountMax'] = isset($xml_printCount->attributes()->max)?
			    			(string)$xml_printCount->attributes()->max : 0;
			    			$ary_addData['lai_printCountIncrementInterval'] = isset($xml_printCount->attributes()->incrementInterval)?
			    			(string)$xml_printCount->attributes()->incrementInterval : 0;
			    		}
		    		}
		    		
		    		$ary_addData['lai_status'] = 1; 
					$ary_addData['lai_addDT'] = Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss');

		    		$this->DBTable_lai->insert($ary_addData);
			    }
		    }
		    else exit('XML loading error.');
		    
		}
		else exit('Book list not exist.');
	}
	
	
	
	
	
}